unit UPharmacyReturn;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, AdvPanel, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, cxGroupBox, DBAdvGlowNavigator,
  cxStyles, dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, UDMPharmacy,
  UDMSettings, cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit,
  cxDBLookupEdit, cxDBLookupComboBox, Vcl.StdCtrls, cxCalendar, cxDBEdit,
  cxCheckBox, cxSpinEdit, Vcl.Menus, cxButtons, UDMPerson, RzButton, RzPanel,
  frxClass, frxDBSet;

type
  TFPharmacyReturn = class(TForm)
    AdvPanel1: TAdvPanel;
    AdvPanelStyler1: TAdvPanelStyler;
    cxGroupBox1: TcxGroupBox;
    AdvPanel2: TAdvPanel;
    DBAdvGlowNavigator1: TDBAdvGlowNavigator;
    AdvPanel3: TAdvPanel;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxButton1: TcxButton;
    RzToolbar1: TRzToolbar;
    RzToolButton1: TRzToolButton;
    RzSpacer1: TRzSpacer;
    RzSpacer2: TRzSpacer;
    RzToolButton2: TRzToolButton;
    cxGrid2DBTableView1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    RzSpacer3: TRzSpacer;
    RzSpacer4: TRzSpacer;
    RzToolButton3: TRzToolButton;
    cxGrid2DBTableView1pharma_return_id: TcxGridDBColumn;
    cxGrid2DBTableView1pricing_id: TcxGridDBColumn;
    cxGrid2DBTableView1returned_qty: TcxGridDBColumn;
    cxGrid1DBTableView1pricing_id: TcxGridDBColumn;
    cxGrid1DBTableView1returned_qty: TcxGridDBColumn;
    cxGrid1DBTableView1add: TcxGridDBColumn;
    Label1: TLabel;
    cxDBLookupComboBox1: TcxDBLookupComboBox;
    Label2: TLabel;
    cxDBTextEdit1: TcxDBTextEdit;
    Label3: TLabel;
    cxDBDateEdit1: TcxDBDateEdit;
    cxGrid1DBTableView1current_qty: TcxGridDBColumn;
    cxGrid1DBTableView1qty_in_stock: TcxGridDBColumn;
    his_pharmacy_return_report: TfrxDBDataset;
    ReReturn: TfrxReport;
    procedure DBAdvGlowNavigator1BtnCancel(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnEdit(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnFirst(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnInsert(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnLast(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnNext(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnPost(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnPrior(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnRefresh(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure RzToolButton1Click(Sender: TObject);
    procedure RzToolButton2Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure cxDBLookupComboBox1PropertiesEditValueChanged(Sender: TObject);
    procedure RzToolButton3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPharmacyReturn: TFPharmacyReturn;

implementation

{$R *.dfm}

procedure TFPharmacyReturn.cxButton1Click(Sender: TObject);
  var
    virtual_rec_count : Integer;
    i : integer;
begin
  if  DMPharmacy.virtual_pharmacy_return_sub.RecordCount > 0 then
  begin
    virtual_rec_count := DMPharmacy.virtual_pharmacy_return_sub.RecordCount;
    DMPharmacy.virtual_pharmacy_return_sub.First;
    for i := 0 to virtual_rec_count -1 do
    BEGIN
      DMPharmacy.his_pharmacy_stock_sub.Locate('pricing_id',DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pricing_id').Value,[]);
      if DMPharmacy.virtual_pharmacy_return_sub.FieldByName('returned_qty').Value > DMPharmacy.his_pharmacy_stock_sub.FieldByName('total_unit_qty').Value then
      BEGIN
        ShowMessage('The Returned Quantity should be less than the quantity in stock');
      END
      ELSE
      BEGIN
        if DMPharmacy.virtual_pharmacy_return_sub.FieldByName('add').Value = 1 then
        begin
          DMPharmacy.his_pharmacy_stock_sub.Edit;
          DMPharmacy.his_pharmacy_stock_sub.FieldByName('total_unit_qty').Value := DMPharmacy.his_pharmacy_stock_sub.FieldByName('total_unit_qty').Value - DMPharmacy.virtual_pharmacy_return_sub.FieldByName('returned_qty').Value;
          DMPharmacy.his_pharmacy_stock_sub.Post;
          DMPharmacy.his_pharmacy_stock_sub.RefreshRecord;

          DMPharmacy.his_pharmacy_stock.Locate('parmacy_stock_id',DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pharmacy_stock_id').Value,[]);
          DMPharmacy.his_pharmacy_stock.Edit;
          DMPharmacy.his_pharmacy_stock.FieldByName('qty_in_stock').Value := DMPharmacy.his_pharmacy_stock.FieldByName('qty_in_stock').Value - DMPharmacy.virtual_pharmacy_return_sub.FieldByName('returned_qty').Value;
          DMPharmacy.his_pharmacy_stock.Post;
          DMPharmacy.his_pharmacy_stock.RefreshRecord;

          DMPharmacy.his_pharmacy_return_sub.Insert;
          DMPharmacy.his_pharmacy_return_sub.FieldByName('pharma_return_id').Value := DMPharmacy.his_pharmacy_return.FieldByName('pharma_return_id').Value;
          DMPharmacy.his_pharmacy_return_sub.FieldByName('pricing_id').Value := DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pricing_id').Value;
          DMPharmacy.his_pharmacy_return_sub.FieldByName('returned_qty').Value := DMPharmacy.virtual_pharmacy_return_sub.FieldByName('returned_qty').Value;
          DMPharmacy.his_pharmacy_return_sub.Post;
          DMPharmacy.his_pharmacy_return_sub.RefreshRecord;
        end;
      END;
      DMPharmacy.virtual_pharmacy_return_sub.Next;
    END;
  end;
  DMPharmacy.virtual_pharmacy_return_sub.Clear;
  DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_q.Active := False;
  DMPharmacy.his_pharmacy_return_report.Refresh;
end;

procedure TFPharmacyReturn.cxDBLookupComboBox1PropertiesEditValueChanged(
  Sender: TObject);
  var
    rec_count : Integer;
    i : integer;
begin
  DMPharmacy.his_pharmacy_supplier_invoice.Locate('pharmacy_supplier_invoice_id',cxDBLookupComboBox1.EditValue,[]);
  DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_for_return.Refresh;

  DMPharmacy.his_pharmacy_stock_return_sub_items.Active := False;
  DMPharmacy.his_pharmacy_stock_return_sub_items.ParamByName('b').Value := Self.cxDBLookupComboBox1.EditValue;
  DMPharmacy.his_pharmacy_stock_return_sub_items.Active := True;

  rec_count := DMPharmacy.his_pharmacy_stock_return_sub_items.RecordCount;
  DMPharmacy.virtual_pharmacy_return_sub.Clear;
  for i := 1 to rec_count do
  begin
    DMPharmacy.virtual_pharmacy_return_sub.Insert;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pharma_return_id').Value := DMPharmacy.his_pharmacy_return.FieldByName('pharma_return_id').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pricing_id').Value := DMPharmacy.his_pharmacy_stock_return_sub_items.FieldByName('pricing_id').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('returned_qty').Value := '1';
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pharmacy_stock_id').Value := DMPharmacy.his_pharmacy_stock_return_sub_items.FieldByName('parmacy_stock_id').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('current_qty').Value := DMPharmacy.his_pharmacy_stock_return_sub_items.FieldByName('total_unit_qty').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('qty_in_stock').Value := DMPharmacy.his_pharmacy_stock_return_sub_items.FieldByName('qty_in_stock').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('add').Value := False;
    DMPharmacy.virtual_pharmacy_return_sub.Post;
    DMPharmacy.his_pharmacy_stock_return_sub_items.Next;
  end;


end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnCancel(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := false;
  DMPharmacy.his_pharmacy_return.Cancel;
  Self.AdvPanel3.Enabled := False;
end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnEdit(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := true;
  DMPharmacy.his_pharmacy_return.Edit;
  Self.AdvPanel3.Enabled := TRUE;

end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnFirst(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := false;
  DMPharmacy.his_pharmacy_return.First;
  Self.AdvPanel3.Enabled := False;

end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnInsert(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := true;
  DMPharmacy.his_pharmacy_return.Insert;
  Self.AdvPanel3.Enabled := TRUE;
end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnLast(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := false;
  DMPharmacy.his_pharmacy_return.Last;
  Self.AdvPanel3.Enabled := False;
end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnNext(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := false;
  DMPharmacy.his_pharmacy_return.Next;
  Self.AdvPanel3.Enabled := False;
end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnPost(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := true;
  DMPharmacy.his_pharmacy_return.Post;
  DMPharmacy.his_pharmacy_return.RefreshRecord;
  Self.AdvPanel1.Enabled := false;
  Self.AdvPanel3.Enabled := True;
end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnPrior(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := false;
  DMPharmacy.his_pharmacy_return.Prior;
  Self.AdvPanel3.Enabled := False;
end;

procedure TFPharmacyReturn.DBAdvGlowNavigator1BtnRefresh(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := false;
  DMPharmacy.his_pharmacy_return.RefreshRecord;
  Self.AdvPanel3.Enabled := False;
end;

procedure TFPharmacyReturn.FormActivate(Sender: TObject);
begin
  DMPharmacy.his_pharmacy_supplier_invoice.Refresh;
  DMPharmacy.his_pharmacy_stock.Refresh;
  DMPharmacy.his_pharmacy_supplier_invoice.Locate('pharmacy_supplier_invoice_id',cxDBLookupComboBox1.EditValue,[]);
  DMPharmacy.his_pharmacy_return_sub.Refresh;
end;

procedure TFPharmacyReturn.RzToolButton1Click(Sender: TObject);
begin
  Self.AdvPanel1.Enabled := true;
  DMPharmacy.his_pharmacy_return.Insert;
  Self.AdvPanel3.Enabled := TRUE;
end;

procedure TFPharmacyReturn.RzToolButton2Click(Sender: TObject);
  var
    rec_count : Integer;
    i : integer;
begin
  Self.AdvPanel3.Enabled := TRUE;
  DMPharmacy.his_pharmacy_stock_return_sub_items.Active := False;
  DMPharmacy.his_pharmacy_stock_return_sub_items.ParamByName('b').Value := Self.cxDBLookupComboBox1.EditValue;
  DMPharmacy.his_pharmacy_stock_return_sub_items.Active := True;


  rec_count := DMPharmacy.his_pharmacy_stock_return_sub_items.RecordCount;
  DMPharmacy.virtual_pharmacy_return_sub.Clear;
  for i := 1 to rec_count do
  begin
    DMPharmacy.virtual_pharmacy_return_sub.Insert;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pharma_return_id').Value := DMPharmacy.his_pharmacy_return.FieldByName('pharma_return_id').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pricing_id').Value := DMPharmacy.his_pharmacy_stock_return_sub_items.FieldByName('pricing_id').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('returned_qty').Value := '1';
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('pharmacy_stock_id').Value := DMPharmacy.his_pharmacy_stock_return_sub_items.FieldByName('parmacy_stock_id').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('current_qty').Value := DMPharmacy.his_pharmacy_stock_return_sub_items.FieldByName('total_unit_qty').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('qty_in_stock').Value := DMPharmacy.his_pharmacy_stock_return_sub_items.FieldByName('qty_in_stock').Value;
    DMPharmacy.virtual_pharmacy_return_sub.FieldByName('add').Value := False;
    DMPharmacy.virtual_pharmacy_return_sub.Post;
    DMPharmacy.his_pharmacy_stock_return_sub_items.Next;
  end;

end;

procedure TFPharmacyReturn.RzToolButton3Click(Sender: TObject);
begin
  Self.ReReturn.ShowReport();
end;

end.
